package cn.hh.org.controller;

import cn.hh.basic.util.AjaxResult;
import cn.hh.basic.util.PageList;
import cn.hh.org.domain.Employee;
import cn.hh.org.query.EmpQuery;
import cn.hh.org.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    private IEmpService empService;
    /*编辑*/
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        /*通过id判断是新增还是删除*/
        try {
            System.out.println("++++++++++++"+employee);
            if (employee.getId()==null){
                empService.add(employee);
            }else {
                empService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*反馈操作失败，及具体错误*/
            AjaxResult ajaxResult = AjaxResult.me().setMessage("操作失败" + e.getMessage());
            return ajaxResult;
        }
    }
    /*删除*/
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id")Long id){
        try {
            empService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*反馈操作失败，及具体错误*/
            AjaxResult ajaxResult = AjaxResult.me().setMessage("操作失败" + e.getMessage());
            return ajaxResult;
        }
    }
    /*批量删除*/
    @PatchMapping
    public AjaxResult deleteByIds(@RequestBody Long[] ids){
        try {
            empService.deleteByIds(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*反馈操作失败，及具体错误*/
            AjaxResult ajaxResult = AjaxResult.me().setMessage("操作失败" + e.getMessage());
            return ajaxResult;
        }
    }
    /*查询所有*/
    @GetMapping
    public List<Employee> loadAll(){
        return empService.loadAll();
    }
    /*分页*/
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmpQuery query){
        return empService.queryPage(query);
    }
}
